package com.littcore.saas.rest.assistant;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.jsondoc.core.annotation.Api;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.annotation.ApiQueryParam;
import org.jsondoc.core.annotation.ApiResponseObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.littcore.common.Utility;
import com.littcore.dao.page.IPageList;
import com.littcore.dao.ql.PageParam;
import com.littcore.exception.NotLoginException;
import com.littcore.saap.core.common.SaapConstants.TodoStatus;
import com.littcore.saap.core.web.util.LoginUtils;
import com.littcore.saap.personal.po.Todo;
import com.littcore.saap.personal.service.ITodoService;
import com.littcore.saap.personal.vo.TodoVo;
import com.littcore.util.DateUtils;

@Api(name = "待办事项接口", description = "增删改查用户的待办事项")
@Controller
public class TodoServiceController {

    @Resource
    private ITodoService todoService;
    
    @ApiMethod(description = "我的待办; 'filter'参数可为done、today、overdue")
    @ApiResponseObject
    @ResponseBody
    @RequestMapping
    public List listAllTodo(@ApiQueryParam(name = "filter") @RequestParam(required=false) String filter) throws NotLoginException
    {
      PageParam pageParam = new PageParam();
      pageParam.addCond("createBy",LoginUtils.getLoginOpId().intValue());
      if("done".equals(filter))
      {
        pageParam.addCond("status", TodoStatus.DONE);
      }
      else if("today".equals(filter))
      {
        pageParam.addCond("startTodoTime", DateUtils.getStartOfDay(new Date()));
        pageParam.addCond("endTodoTime", DateUtils.getEndOfDay(new Date()));
      }
      else if("overdue".equals(filter))
      {
        //只有NEW跟InProgress状态的才在overdue中显示
        pageParam.addCond("status", TodoStatus.NEW);
        pageParam.addCond("statusExt", TodoStatus.IN_PROGRESS);
        pageParam.addCond("endTodoTime", new Date());
      }
      
      IPageList pageList = todoService.listPage(pageParam);
      
      return pageList.getRsList();
    }
    
    @ApiMethod(description = "添加待办")
    @ApiResponseObject
    @ResponseBody
    @RequestMapping 
    public TodoVo save(@ApiQueryParam(name = "content",description="内容")@RequestParam(required=true) String content) throws Exception {
        Todo todo = new Todo();
        todo.setContent(content);
        todo.setIsPublic(false);
        todo.setIsNeedRemind(true);
        TodoVo vo = todoService.saveAndReturn(todo);
        return vo;
    }
   
    
    /**
     * Update.
     * @param request 
     * @param modelMap
     * @throws Exception 
     */
    @ApiMethod(description = "更新内容")
    @ApiResponseObject
    @ResponseBody
    @RequestMapping 
    public void updateContent(@ApiQueryParam(name = "id")@RequestParam(required=true) Integer id, 
            @ApiQueryParam(name = "content")@RequestParam(required=true) String content) throws Exception
    {
      Todo todo = todoService.load(id);
      todo.setContent(content);
      
      todoService.update(todo);
    }
    
    /**
     * Update status.
     *
     * @param id the id
     * @param status the status
     * @throws Exception the exception
     */
    @ApiMethod(description = "更新状态")
    @ApiResponseObject
    @ResponseBody
    @RequestMapping   
    public void updateStatus(@ApiQueryParam(name = "id")@RequestParam(required=true) Integer id,
            @ApiQueryParam(name = "id")@RequestParam(required=true ) Integer status) throws Exception
    {   
      todoService.updateStatus(id, status);
    }
    
    /**
     * 更新公开状态.
     *
     * @param id the id
     * @param isPublic the is public
     */
    @ApiMethod(description = "更新公开状态")
    @ApiResponseObject
    @ResponseBody
    @RequestMapping  
    public void updatePublic(@ApiQueryParam(name = "id")@RequestParam(required=true) Integer id, 
            @ApiQueryParam(name = "isPublic")@RequestParam(required=true) boolean isPublic) 
    {   
      todoService.updatePublic(id, isPublic);
    }
    
    @ApiMethod(description = "更新提醒时间")
    @ApiResponseObject
    @ResponseBody
    @RequestMapping  
    public void updateRemindDatetime(@ApiQueryParam(name = "id")@RequestParam(required=true) Integer id, 
            @ApiQueryParam(name = "remidDateTime")@RequestParam(required=true) String remindDateTime) 
    {   
        
      todoService.updateRemindDateTime(id, Utility.parseDateTime(remindDateTime));
    }
    
    
    @ApiMethod(description = "更新期限时间")
    @ApiResponseObject
    @ResponseBody
    @RequestMapping  
    public void updateTodoDatetime(@ApiQueryParam(name = "id")@RequestParam(required=true) Integer id, 
            @ApiQueryParam(name = "todoDateTime")@RequestParam(required=true) String todoDateTime) 
    {   
        
      todoService.updateTodoDatetime(id, Utility.parseDateTime(todoDateTime));
    }
    
    @ApiMethod(description = "删除")
    @ApiResponseObject
    @ResponseBody
    @RequestMapping 
    public void delete(@ApiQueryParam(name = "id")@RequestParam(required=true)  Integer id) throws Exception
    {
      todoService.delete(id);
    }

}
